﻿namespace MicroCloud.Entity
{
    /// <summary>
    /// <see cref="IndexBuilder"/> 的关系数据库特定扩展方法
    /// </summary>
    public static class RelationalBuilderExtensions
    {
        #region 设置外键
        /// <summary>
        /// 设置外键
        /// </summary>
        /// <remarks>
        /// 请参阅 <see href="https://aka.ms/efcore-docs-modeling">建模实体类型和关系</see> 以获取更多信息和示例。
        /// </remarks>
        /// <typeparam name="TEntity">此关系中的主体实体类型</typeparam>
        /// <typeparam name="TRelatedEntity">此关系中的依赖实体类型</typeparam>
        /// <param name="referenceReferenceBuilder">用于配置关系的生成器</param>
        /// <param name="name">外键约束的名称</param>
        /// <param name="required">是否必须(默认true)</param>
        /// <returns>相同的构建器实例，以便可以链接多个调用。</returns>
        public static ReferenceReferenceBuilder<TEntity, TRelatedEntity> SetForeignKey<TEntity, TRelatedEntity>(this ReferenceReferenceBuilder<TEntity, TRelatedEntity> referenceReferenceBuilder,
          string name, bool required = true)
          where TEntity : class
          where TRelatedEntity : class
        {
            Check.NotNullOrEmpty(name, nameof(name));

            var entityName = typeof(TEntity).Name;
            var foreignKeyName = $"FK_{entityName}_{name}";

            referenceReferenceBuilder.HasConstraintName(foreignKeyName).IsRequired(required);
            return referenceReferenceBuilder;
        }

        /// <summary>
        /// 设置外键
        /// </summary>
        /// <remarks>
        /// 请参阅 <see href="https://aka.ms/efcore-docs-modeling">建模实体类型和关系</see> 以获取更多信息和示例。
        /// </remarks>
        /// <param name="referenceCollectionBuilder">用于配置关系的生成器</param>
        /// <param name="name">外键约束的名称</param>
        /// <param name="required">是否必须(默认true)</param>
        /// <returns>相同的构建器实例，以便可以链接多个调用。</returns>
        /// <typeparam name="TEntity">此关系中的主体实体类型</typeparam>
        /// <typeparam name="TRelatedEntity">此关系中的依赖实体类型</typeparam>
        public static ReferenceCollectionBuilder<TEntity, TRelatedEntity> SetForeignKey<TEntity, TRelatedEntity>(this ReferenceCollectionBuilder<TEntity, TRelatedEntity> referenceCollectionBuilder,
            string name, bool required = true)
            where TEntity : class
            where TRelatedEntity : class
        {
            Check.NotNullOrEmpty(name, nameof(name));
            
            var entityName = typeof(TRelatedEntity).Name;
            var foreignKeyName = $"FK_{entityName}_{name}";
            
            referenceCollectionBuilder.HasConstraintName(foreignKeyName).IsRequired(required);
            return referenceCollectionBuilder;
        }

        #endregion

        #region 设置索引
        /// <summary>
        /// 设置索引
        /// </summary>
        /// <typeparam name="TEntity">实体类型</typeparam>
        /// <param name="indexBuilder">索引构建器</param>
        /// <param name="name">索引名称(不带前缀的名称，如：IX_TB_[NAME])</param>
        /// <param name="unique">是否唯一索引(默认false)</param>
        /// <returns>索引构建器</returns>
        public static IndexBuilder<TEntity> SetIndex<TEntity>(this IndexBuilder<TEntity> indexBuilder, string name, bool unique = false)
        {
            Check.NotNullOrEmpty(name, nameof(name));

            var entityName = typeof(TEntity).Name;
            var indexName = unique ? $"IDX_{entityName}_{name}" : $"IX_{entityName}_{name}";

            indexBuilder.HasDatabaseName(indexName).IsUnique(unique).HasFilter(null);
            return indexBuilder;
        }

        #endregion

    }
}